package com.example.springboot.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.springboot.entity.Follow;
import com.example.springboot.entity.dto.FollowDto;
import com.example.springboot.mapper.FollowMapper;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.FollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class FollowServiceImpl implements FollowService {
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    @Transactional
    public void follow(FollowDto followDto) {
        Follow follow = BeanUtil.copyProperties(followDto,Follow.class);
        if(followMapper.isEmpty(follow) == null){
            followMapper.insertFollow(follow);
            userMapper.increFansNum(followDto.getFollowedId());
        }else{
            followMapper.updateFollow(follow);
            if(follow.getIsFollow() == 1){
                userMapper.increFansNum(followDto.getFollowedId());
            }else{
                userMapper.decreFansNum(followDto.getFollowedId());
            }
        }
    }

    @Override
    public Follow getIsFollow(Integer followId, Integer followedId) {
        Follow follow = new Follow();
        follow.setFollowId(followId);
        follow.setFollowedId(followedId);

        return followMapper.isEmpty(follow);
    }
}
